package org.pi.common.geom

class Straight(var n: Vector3f) {
  n = n / Math.sqrt( (n(0)*n(0)+n(1)*n(1)).doubleValue() ).floatValue(); 

  def intersect(s: Straight) = Point( n crossprod s.n )
  def signedDistance(p: Point) = n dotprod p.homogeneousCoordinates 
  def distance(p: Point) = Math.abs( signedDistance(p).doubleValue() ).floatValue() 
  override def toString() = n.toString()
}

object Straight {
  def apply(n: Vector3f) = new Straight(n)
  def apply(p1: Point, p2: Point) = new Straight(p1.homogeneousCoordinates crossprod p2.homogeneousCoordinates)
}

object StraightMain {
  def main(args: Array[String]) {
    var s1 = Straight( Point(1,1), Point(1,10) )
    var s2 = Straight( Point(-1,5), Point(10,5) )
    var p1 = s1 intersect s2 //kann man lesen wie: var p1 = s1.intersect(s2)
    println("p1="+p1+", must be 1,5")
    println("dist s1 (0,0)="+(s1 distance Point(0,0))+", must be 1.0")

    var s3 = Straight( Point(12.3f,11.1f), Point(0,0) )
    var s4 = Straight( Point(12.3f,11.1f), Point(10,5) )
    var p2 = s3 intersect s4
    println("p2="+p2+", must be 12.3,11.1")
    println("dist p2="+(s3 distance p2)+", must be 0.0")
  }
}
